
import java.lang.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.border.TitledBorder;
import java.sql.*;
import java.util.ArrayList;

public class SelectionListe extends JFrame implements ActionListener {

  private JPanel pRech;
  private JPanel pRes;
  private JPanel pOutil;
  private JTable tRes;
  private JLabel lNom;
  private JTextField tNom;
  private JLabel lDate;
  private JTextField tDate;
  private JLabel lOrd;
  private JRadioButton rASC;
  private JRadioButton rDESC;
  private JButton bRech;
  private JButton bMod;
  private JButton bSup;
  private JButton bCrea;
  private JButton bQuit;
  private Container contenu;
  private ButtonGroup bg;
  private JScrollPane scroll;

  public SelectionListe() {
    super("Listes");
    this.setLayout(null);

    this.pRech = new JPanel(null, false);
    this.pRech.setBorder(new TitledBorder(null, "Recherche", TitledBorder.LEFT, TitledBorder.DEFAULT_POSITION));

    this.pRes = new JPanel(null, false);
    this.pRes.setBorder(new TitledBorder(null, "Resultat", TitledBorder.LEFT, TitledBorder.DEFAULT_POSITION));

    this.pOutil = new JPanel(null, false);
    this.pOutil.setBorder(new TitledBorder(null, "Outils", TitledBorder.LEFT, TitledBorder.DEFAULT_POSITION));

    // Chargement des listes

    this.loadTableau(true);

    this.lNom = new JLabel("Nom de la liste: ");
    this.lDate = new JLabel("Date butoire: ");
    this.lOrd = new JLabel("Par ordre: ");

    this.tNom = new JTextField();
    this.tDate = new JTextField();

    this.bg = new ButtonGroup();
    this.rASC = new JRadioButton("Croissant", true);
    this.rDESC = new JRadioButton("Decroissant");
    this.bg.add(this.rASC);
    this.bg.add(this.rDESC);

    this.bRech = new JButton("Recherche");
    this.bRech.addActionListener(this);

    this.bMod = new JButton("Modifier");
    this.bMod.addActionListener(this);

    this.bCrea = new JButton("Créer");
    this.bCrea.addActionListener(this);

    this.bSup = new JButton("Supprimer");
    this.bSup.addActionListener(this);

    this.bQuit = new JButton("Quitter");
    this.bQuit.addActionListener(this);

    this.contenu = this.getContentPane();
    this.contenu.setLayout(null);

    this.pRech.add(this.lNom);
    this.pRech.add(this.tNom);
    this.pRech.add(this.tDate);
    this.pRech.add(this.lDate);
    this.pRech.add(this.lOrd);
    this.pRech.add(this.rASC);
    this.pRech.add(this.rDESC);
    this.pRech.add(this.bRech);
    this.pOutil.add(this.bMod);
    this.pOutil.add(this.bQuit);
    this.pOutil.add(this.bSup);
    this.pOutil.add(this.bCrea);
    this.contenu.add(this.pRes);
    this.contenu.add(this.pRech);
    this.contenu.add(this.pOutil);

    this.pRech.setBounds(10, 10, 565, 175);
    this.pRes.setBounds(10, 200, 450, 300);
    this.pOutil.setBounds(475, 200, 150, 300);
    this.lNom.setBounds(15, 20, 100, 20);
    this.tNom.setBounds(140, 20, 100, 20);
    this.lDate.setBounds(15, 50, 100, 20);
    this.tDate.setBounds(140, 50, 100, 20);
    this.lOrd.setBounds(15, 80, 100, 20);
    this.rASC.setBounds(120, 80, 100, 20);
    this.rDESC.setBounds(220, 80, 100, 20);
    this.bRech.setBounds(90, 120, 105, 20);
    this.bCrea.setBounds(25, 30, 100, 20);
    this.bMod.setBounds(25, 80, 100, 20);
    this.bSup.setBounds(25, 130, 100, 20);
    this.bQuit.setBounds(25, 180, 100, 20);


    this.setBounds(50, 50, 650, 600);
    this.setVisible(true);
    this.setResizable(false);
  }

  public void actionPerformed(ActionEvent e) {

    if (e.getSource() == this.bCrea) {
      AjoutListe fenAjout = new AjoutListe();
      this.loadTableau(false);
    } else if (e.getSource() == this.bRech) {
    } else if (e.getSource() == this.bMod) {
      int selection = this.tRes.getSelectedRow();
      if (selection >= 0) {
        if (this.tRes.getSelectedRowCount() == 1) {
          GererListe gl = new GererListe(this.getSelectedListe(selection));
        } else {
          JOptionPane.showMessageDialog(this, "Vous ne pouvez sélectionner qu'une seule liste", "Modifier la liste", JOptionPane.ERROR_MESSAGE);
        }
      } else {
        JOptionPane.showMessageDialog(this, "Vous devez sélectionner une liste", "Modifier la liste", JOptionPane.ERROR_MESSAGE);
      }
    }

    if (e.getSource() == this.bQuit) {
      this.dispose();
    }

    if (e.getSource() == this.bSup) {
      int selection = this.tRes.getSelectedRow();
      if (selection >= 0) {
        if (this.tRes.getSelectedRowCount() == 1) {
          int confirm = JOptionPane.showConfirmDialog(this, "Êtes-vous sûr de vouloir supprimer cette liste ?", "Supprimer la liste", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
          if (confirm == JOptionPane.OK_OPTION) {
            try {
              getSelectedListe(selection).delete();
              loadTableau(false);
              JOptionPane.showMessageDialog(this, "La liste a bien été supprimée.", "Supprimer la liste", JOptionPane.INFORMATION_MESSAGE);
            } catch (Exception ex) {
              JOptionPane.showMessageDialog(this, "La suppression n'a pas pu aboutir.", "Supprimer la liste", JOptionPane.ERROR_MESSAGE);
            }
          }
        } else {
          JOptionPane.showMessageDialog(this, "Vous ne pouvez sélectionner qu'une seule liste.", "Supprimer la liste", JOptionPane.ERROR_MESSAGE);
        }
      } else {
        JOptionPane.showMessageDialog(this, "Vous devez sélectionner une liste.", "Supprimer la liste", JOptionPane.ERROR_MESSAGE);
      }
    }
  }

  public Liste getSelectedListe(int selection) {
    return new Liste(Integer.parseInt((String) this.tRes.getValueAt(selection, 0)), (String) this.tRes.getValueAt(selection, 1), new Date((String) this.tRes.getValueAt(selection, 2)));
  }

  public void loadTableau(boolean callFromConstructor) {
    String[] colonnes = new String[]{"Id", "Nom", "Date butoire"};

    ArrayList<Liste> listes = null;
    try {
      listes = Liste.chargerListes();
    } catch (SQLException e) {
      JOptionPane.showMessageDialog(this, "Erreur dans le chargement des listes \n" + e, "Erreur BDD", JOptionPane.ERROR_MESSAGE);
    }

    Object[][] listesTab = new String[listes.size()][3];
    int i = 0;
    for (Liste l : listes) {
      listesTab[i] = new String[]{String.valueOf(l.getListID()), l.getNom(), l.getDateButoire().toString()};
      i++;
    }

    // On met à jour le tableau en enlevant l'ancien et en le reconstruisant

    if (!callFromConstructor) {
      this.pRes.remove(this.scroll);
    }

    this.tRes = new JTable(listesTab, colonnes);
    this.scroll = new JScrollPane(this.tRes);
    this.scroll.createVerticalScrollBar();

    this.pRes.add(this.scroll);

    this.scroll.setBounds(10, 30, 430, 250);
    this.scroll.createVerticalScrollBar();
  }
}
